package com.zhongx.xingcd.zxxcd.controller;

import com.zhongx.xingcd.zxxcd.common.ExcelUtils;
import com.zhongx.xingcd.zxxcd.service.UserLoginService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class WhiteNumberController {


    //登陆成功
    @RequestMapping("/goage")
    public String goage() {
        return "zxxcd/biao";
    }
    /**
     * 下载excel文件
     * @param response
     */
    @RequestMapping("/downfile")
    public void templateDownload(HttpServletResponse response) {
        try {
            /*ClassLoader.getSystemResource("static/temp/关闭号码.xlsx");
            System.out.println();
            File file = new File("static/temp/关闭号码.xlsx");
            InputStream fis = new FileInputStream(file);*/

            InputStream fis = ClassLoader.getSystemResourceAsStream("static/temp/关闭号码.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook(fis);
            response.setContentType("application/binary;charset=ISO8859-1");
            String fileName = java.net.URLEncoder.encode("测试excel", "UTF-8");
            response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");
            ServletOutputStream out = null;
            out = response.getOutputStream();
            workbook.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭文件输出流
        }
        return;
    }

    @RequestMapping(value = "/importExcel",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String ImportExcel(MultipartFile file) throws Exception {
        //这里创建json对象，实测用map的话，json数据会有问题
        InputStream inputStream = file.getInputStream();
        System.out.println("sss  --"+file.getName()+file.getSize()+file.getContentType()+file.getOriginalFilename());

        JSONObject jsonObject = new JSONObject();
        List list = ExcelUtils.excelToShopIdList(inputStream);
        System.out.println(list.toString()+"ssb");
        if(ExcelUtils.shorecontroy(list)){
            System.out.println("有重复");
            jsonObject.put("result_data","elcel内票号有重复");
            jsonObject.put("result_code",1);
            jsonObject.put("result_mes","excle导入失败");
            System.out.println(jsonObject.toString());
            return jsonObject.toString();
        }
        if(ExcelUtils.judgeRepeatExcle(list)){
            System.out.println("国际或者国内");
            jsonObject.put("result_data","elcel内有类型错误");
            jsonObject.put("result_code",1);
            jsonObject.put("result_mes","excle导入失败");
            System.out.println(jsonObject.toString());
            return jsonObject.toString();
        }

        jsonObject.put("result_data_size",list.size());
        jsonObject.put("result_data",list);
        jsonObject.put("result_code",0);
        jsonObject.put("result_mes","excle导入成功");
        System.out.println(jsonObject.toString());
        return jsonObject.toString();
    }


}
